# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Allow superusers to bypass hooks.

Return -1 (immediate success, bypassing remaining hooks) if:
- user is in gvn:superusers
- transaction includes gvn:bypass-hooks revprop

The gvn:bypass-hooks is left on the committed revision, as it may be
of interest for auditing purposes see
gvn.hooks.pre_revprop_change.check_gvn_revprops for a hook module that
blocks later changes to this revprop.
"""


import svn.fs


def RunHook(hi, logger):
  """Implements gvn.hooks.runner's RunHook interface

  Returns:
    -1: pass and bypass other hooks
    0: pass
    1: fail
    "string": fail and print string to the user
  """

  if svn.fs.txn_prop(hi.txn, "gvn:bypass-hooks") is None:
    # Bypass not invoked, we're done.
    return 0

  author = svn.fs.txn_prop(hi.txn, "svn:author", hi.pool)
  if (author in hi.super_users
      or hi.super_groups.intersection(hi.userdb.UserGroups(author))):
    logger.info("Superuser %s invoked gvn:bypass-hooks" % (author,))
    # Immediately allow commit, bypassing further hooks.
    return -1

  # Bad luser, block his efforts.
  return ("Non Superuser %s tried to invoke gvn:bypass-hooks" % (author,))
